﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TfsGiggle.Core
{
    /// <summary>
    /// The states a job can be in
    /// </summary>
    [Serializable]
    public enum State
    {
        /// <summary>
        /// None
        /// </summary>
        None,
        /// <summary>
        /// Just created
        /// </summary>
        Waiting,
        /// <summary>
        /// Actively running
        /// </summary>
        Running,
        /// <summary>
        /// Completed sucessfully
        /// </summary>
        Success,
        /// <summary>
        /// Completed with error
        /// </summary>
        Error,
    }

    /// <summary>
    /// Job that is persisted
    /// </summary>
    [Serializable]
    public struct Job
    {
        /// <summary>
        /// team project collection
        /// </summary>
        public string TeamProjectCollectionId;

        /// <summary>
        /// Changeset to process
        /// </summary>
        public int ChangesetId;

        /// <summary>
        /// When was the job submitted
        /// </summary>
        public DateTime? SubmittedDate;

        /// <summary>
        /// When was it created
        /// </summary>
        public DateTime? ChangedDate;

        /// <summary>
        /// Current or persisted state
        /// </summary>
        public State _status;

        /// <summary>
        /// If completed with error, what was the error
        /// </summary>
        public string ErrorMessage;

        /// <summary>
        /// Primary key
        /// </summary>
        public string ID;

        /// <summary>
        /// How many times have I been retried
        /// </summary>
        public int RetryCount;

        /// <summary>
        /// Whats my status
        /// </summary>
        public State Status
        {
            get
            {
                return _status;
            }
            set
            {
                _status = value;
                ChangedDate = DateTime.UtcNow;
            }
        }

        /// <summary>
        /// Create a new job
        /// </summary>
        /// <param name="teamProjectCollectionId"></param>
        /// <param name="changesetId"></param>
        public Job(string teamProjectCollectionId, int changesetId)
        {
            TeamProjectCollectionId = teamProjectCollectionId.ToString();
            ChangesetId = changesetId;
            SubmittedDate = DateTime.UtcNow;
            ChangedDate = DateTime.UtcNow;
            _status = State.Waiting;
            ErrorMessage = "";
            ID = Guid.NewGuid().ToString();
            RetryCount = 0;
        }
    }
}
